package searchstring;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;


public class StringBuscaSpringerBuilder extends AbstractStrategyStringBuscaBuilder{
	private static StringBuscaSpringerBuilder instance;

	private static SearchCriteria searchCriteria;
	
	private StringBuscaSpringerBuilder() {

	}

	/**
	 * This method returns the single instance of the class instance
	 * 
	 * @return {@link StringBuscaSpringerBuilder}
	 */
	public static StringBuscaSpringerBuilder getInstance() {
		if (StringBuscaSpringerBuilder.instance == null) {
			StringBuscaSpringerBuilder.instance = new StringBuscaSpringerBuilder();

			String searchMode = "ab:";
			//String searchMode = null;
			
			StringBuscaSpringerBuilder.searchCriteria = new SearchCriteria();
			StringBuscaSpringerBuilder.searchCriteria.setMinYear("2003");
			StringBuscaSpringerBuilder.searchCriteria.setMaxYear("2013");
			StringBuscaSpringerBuilder.searchCriteria.setSearchMode(searchMode);
			StringBuscaSpringerBuilder.searchCriteria.setLanguage("%22En%22");
			StringBuscaSpringerBuilder.searchCriteria.setContentType("%22Article%22");
			
			String[] areas = {"%22Computer+Science%22"};
			
			StringBuscaSpringerBuilder.searchCriteria.setAreas(Arrays.asList(areas));
			
		}
		return StringBuscaSpringerBuilder.instance;
	}

	public String getWebSource() {
		return "link.springer.com";
	}
	
	/**
	 * 
	 * 
	 * @param search_String
	 * @return
	 */
	public String getURL(String search_String){ 

		String search_String_formatted = null;
		try {
			search_String_formatted = java.net.URLEncoder.encode(search_String, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		SearchCriteria searchCriteria = StringBuscaSpringerBuilder.searchCriteria;
		
		String areasParam = "";
		if(searchCriteria.getAreas().isEmpty() == false){
			areasParam = "&facet-discipline=" + searchCriteria.getAreas().get(0);
		}
		
		return "http://link.springer.com/search" +
				"?facet-start-year="+searchCriteria.getMinYear()+
				"&facet-language="+searchCriteria.getLanguage()+
				"&query="+search_String_formatted+
				"&facet-end-year=" + searchCriteria.getMaxYear()+
				"&showAll=true" +
				areasParam +
				"&facet-content-type="+searchCriteria.getContentType();
		
		
	}

	public Map<String, String> getMapQuestaoPesquisaStringBuilder() {
		Map<String, String> mapQuestaoPesquisaStringBuilder = new LinkedHashMap<String, String>();

		SearchCriteria searchCriteria = StringBuscaSpringerBuilder.searchCriteria;
		
		for (QuestaoPesquisa questaoPesquisa : StringBuscaLoader.getInstance().getQuestoesPesquisa()) {

			String stringBusca = "";
			String separador = "";
			
			if(searchCriteria.getSearchMode() == null){
				for (TokenStringBusca tokenStringBusca : questaoPesquisa.getTokensStringBusca()) {
					stringBusca+= separador + tokenStringBusca.getStringBuscaSinonimos(false);
					separador = " AND ";
				}
			}else{
				for (TokenStringBusca tokenStringBusca : questaoPesquisa.getTokensStringBusca()) {
					stringBusca+= separador + tokenStringBusca.getStringBuscaSinonimos(false, searchCriteria.getSearchMode());
					separador = " AND ";
				}
			}

			mapQuestaoPesquisaStringBuilder.put(questaoPesquisa.getName(), "("+stringBusca+")");
		}

		return mapQuestaoPesquisaStringBuilder;
	}
}
